package com.itheima.social.securitys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.common.utils.IdWorker;
import com.itheima.social.securitys.mapper.CompanySettingsMapper;
import com.itheima.social.securitys.pojo.CompanySettings;
import com.itheima.social.securitys.service.CompanySettingsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author qiankaofei
 * @version 1.0
 * @date 2020/12/6 17:56
 */
@Service
@Transactional
public class CompanySettingsServiceImpl implements CompanySettingsService {

    @Autowired
    private CompanySettingsMapper companySettingsMapper;

    @Autowired
    private IdWorker idWorker;


    /**
     *  根据企业id查询该企业是否设置过社保
     */
    @Override
    public CompanySettings findById(String companyId) {
        QueryWrapper<CompanySettings> queryWrapper=new QueryWrapper<CompanySettings>();
        queryWrapper.eq("company_id",companyId);
        CompanySettings companySettings = companySettingsMapper.selectOne(queryWrapper);
        if(companySettings==null){
            return null;
        }
        return companySettings;
    }

    /**
     *  保存企业设置
     */
    @Override
    public Integer saveSettings(CompanySettings companySettings) {
        companySettings.setIsSettings(1);
        companySettings.setId(String.valueOf(idWorker.nextId()));
        return companySettingsMapper.insert(companySettings);
    }

    /**
     * 制作新的报表
     */
    @Override
    public void saveNewReportSettings(String yearMonth, String companyId) {
        QueryWrapper<CompanySettings> queryWrapper=new QueryWrapper<CompanySettings>();
        queryWrapper.eq("company_id",companyId);
        CompanySettings companySettings=new CompanySettings();
        companySettings.setIsSettings(1);
        companySettings.setCompanyId(companyId);
        companySettings.setDataMonth(yearMonth);
        companySettings.setId(String.valueOf(idWorker.nextId()));
        int count = companySettingsMapper.insert(companySettings);
    }

}
